package sren.serialtest

import com.fazecast.jSerialComm.SerialPort

import java.io.{InputStream, OutputStream}


class SerialObj {

  var currentPort: SerialPort = null

  var os: OutputStream = null
  var is: InputStream = null

  def getPorts(): Seq[SerialPort] = {
    val pts: Seq[SerialPort] = SerialPort.getCommPorts().toList
    return pts
  }

  def connect(port: String, baudRate: Int = 115200): Unit = {
    currentPort = SerialPort.getCommPort(port)
    println((currentPort.getDescriptivePortName, currentPort.getBaudRate, currentPort.getPortDescription, currentPort.getSystemPortName, currentPort.getSystemPortPath))
    currentPort.setBaudRate(baudRate)
    currentPort.openPort()
    this.os = currentPort.getOutputStream
    this.is = currentPort.getInputStream
  }

  def disConnect(): Unit = {
    this.os.close()
    this.is.close()
    currentPort.closePort();
    currentPort = null
  }

}
